//-----------------------------------------------------------------------
// <copyright file="StatuDAOBase.cs" company="FetCo">
//     Copyright (c) FetCo. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using FetCo.Framework;
using FetCo.Framework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ADESSA.Entities;

namespace ADESSA.Data
{    
    public abstract class StatuDAOBase
    {
        #region Common methods
        public virtual Statu CreateStatuFromReader(IDataReader reader)
        {
            Statu item = new Statu();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("StatusId"))) item.StatusId = (int)reader["StatusId"];
                if (!reader.IsDBNull(reader.GetOrdinal("StatusName"))) item.StatusName = (string)reader["StatusName"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateStatuFromReaderException, ex);
            }
            return item;
        }
        #endregion
        
        #region CreateStatu methods
            
        public virtual void CreateStatu(Statu statu)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spStatusInsert");
                
                database.AddInParameter(dbCommand, "@StatusName", DbType.String, statu.StatusName);
                database.AddOutParameter(dbCommand, "@StatusId", DbType.Int32, 4);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
                statu.StatusId = (int)database.GetParameterValue(dbCommand, "@StatusId");
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateStatuException, ex);
            }
        }

        #endregion

        #region UpdateStatu methods
        
        public virtual void UpdateStatu(Statu statu)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spStatusUpdate");            
                
                database.AddInParameter(dbCommand, "@StatusId", DbType.Int32, statu.StatusId);
                database.AddInParameter(dbCommand, "@StatusName", DbType.String, statu.StatusName);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessUpdateStatuException, ex);
            }
        }
        
        #endregion

        #region DeleteStatu methods
        public virtual void DeleteStatu(int statusId)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spStatusDelete");
                
                database.AddInParameter(dbCommand, "@StatusId", DbType.Int32, statusId);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessDeleteStatuException, ex);
            }
        }

        #endregion

        #region GetStatu methods
        
        public virtual Statu GetStatu(int statusId)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spStatusGet");
                
                database.AddInParameter(dbCommand, "@StatusId", DbType.Int32, statusId);
                
                Statu statu =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    if (reader.Read())
                    {
                        statu = CreateStatuFromReader(reader);
                        reader.Close();
                    }
                }
                return statu;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetStatuException, ex);
            }
        }
        
        #endregion

        #region GetStatuList methods
        public virtual StatuCollection GetStatuList(StatuColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spStatusGetList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                StatuCollection statuCollection = new StatuCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        Statu statu = CreateStatuFromReader(reader);
                        statuCollection.Add(statu);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return statuCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetStatuListException, ex);
            }
        }
        
        public virtual StatuCollection GetStatuList(StatuColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetStatuList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
		
		  #region GetStatuList methods
        public virtual StatuCollection GetStatuListByParam(object orderBy, OrderDirection orderDirection, int page, int pageSize,string Criteria, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spStatusGetListByParam");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
				database.AddInParameter(dbCommand, "@Criteria",  DbType.String, Criteria);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                StatuCollection statuCollection = new StatuCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        Statu statu = CreateStatuFromReader(reader);
                        statuCollection.Add(statu);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return statuCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetStatuListException, ex);
            }
        }
        
        public virtual StatuCollection GetStatuListByParam(object orderBy, OrderDirection orderDirection,string Criteria)
        {            
            int totalRecords = 0;
            return GetStatuListByParam(orderBy, orderDirection, 0, 0,Criteria, out totalRecords);
        }
        
        #endregion
    }
}